package com.henu.acmrating.crawler;

import com.henu.acmrating.pojo.Problem;
import com.henu.acmrating.pojo.UserOj;
import com.henu.acmrating.service.ProblemService;
import com.henu.acmrating.service.UserOjService;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
public class BaseProcessor {

    public static String[] getUrls(UserOjService userOjService, String ojName, String baseUrl) {
        List<UserOj> list = userOjService.selectByOjName(ojName);
        String[] urls = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            UserOj userOj = list.get(i);
            urls[i] = baseUrl + userOj.getAccount();
            if (!userOj.getLastId().equals(userOj.getMaxId())) {
                userOj.setLastId(userOj.getMaxId());
                userOjService.updateByPrimaryKey(userOj);
            }
        }
        return urls;
    }

    public static List<Request> getGraphQLs(UserOjService userOjService, String ojName, String baseUrl) {
        List<UserOj> list = userOjService.selectByOjName(ojName);
        List<Request> requests = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            UserOj userOj = list.get(i);

            Request request = new Request(baseUrl);
            request.setMethod(HttpConstant.Method.POST);
            request.addHeader("Referer-Type", "https://leetcode-cn.com/u/" + userOj.getAccount());
            request.setRequestBody(HttpRequestBody.json(" { \"variables\": { \"userSlug\": \"" + userOj.getAccount() + "\" }, \"query\": \"query recentSubmissions($userSlug: String!) {recentSubmissions(userSlug: $userSlug) {id status lang question {  questionFrontendId  title  translatedTitle  titleSlug  __typename  }  submitTime   runtime memory}}\"}", "utf-8"));

            requests.add(request);

            if (!userOj.getLastId().equals(userOj.getMaxId())) {
                userOj.setLastId(userOj.getMaxId());
                userOjService.updateByPrimaryKey(userOj);
            }
        }
        return requests;
    }


    public static Boolean isContain(String status) {
        return status != null && (status.contains("Accepted") || status.contains("Happy New Year!"));
    }



    public static void insertDataBase(ProblemService problemService, UserOj userOj, Set<String> acProblemSet,
                                      List<Problem> problemList, Integer maxRunId, UserOjService userOjService) {
        if(problemList.isEmpty()) return ;
        acProblemSet.add("#lxl#zhanweifu");
        final List<Problem> alreadyList = problemService.selectByUserIdAndProblemUrls(userOj.getUserId(), acProblemSet);
        // 插入数据库
        problemService.batchInsert(problemList);
        // 更新 通过数
        userOj.setSolved(userOj.getSolved() + acProblemSet.size() - alreadyList.size());
        userOj.setSubmissions(userOj.getSubmissions() + problemList.size());
        if (maxRunId > userOj.getMaxId()) {
            userOj.setMaxId(maxRunId);
        }
        userOjService.updateByPrimaryKeySelective(userOj);
    }


}
